package com.ustcinfo.ishare.esClient;

import com.ustcinfo.ishare.utils.XmlProperties;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Shinelon on 2017/12/27.
 * 不带认证的 es 客户端初始化
 */
public class ESClientWithoutAuthority {
    private static final Logger LOGGER = LoggerFactory.getLogger(ESClientWithoutAuthority.class);

    /**
     * TransportClient 客户端
     */
    private TransportClient transportClient = null;
    /**
     * 是否初始化
     */
    private volatile boolean inited = false;

    private boolean client_transport_sniff;
    private String transport_addresses;
    /**
     * es 集群名称
     */
    private String cluster_name;

    public synchronized TransportClient init(){
        if (!inited) {
            try {
                Map<String, String> config = XmlProperties.loadFromXml("elasticsearch.xml");
                if (config == null) {
                    LOGGER.error("load xml error");
                    System.out.println("配置文件为空！");
                    return null;
                }
                Iterator<Map.Entry<String, String>> iterator = config.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, String> next = iterator.next();
                    if (next.getKey().equals("client.transport.sniff")) {
                        client_transport_sniff = Boolean.parseBoolean(next.getValue());
                    }
                    if (next.getKey().equals("transport.addresses")) {
                        transport_addresses = next.getValue();
                    }
                    if (next.getKey().equals("cluster.name")) {
                        cluster_name = next.getValue();
                    }
                }

                Settings settings = Settings
                        .builder()
                        .put("cluster.name",cluster_name)
                        .put("client.transport.sniff", client_transport_sniff)
                        .build();

                TransportClient client = new PreBuiltTransportClient(settings);
                this.transportClient = client;

                String[] addresses = config.get("transport.addresses").split(",");
                for (String address : addresses) {
                    String[] hostAndPort = address.split(":");
                    client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostAndPort[0]), Integer.valueOf(hostAndPort[1])));
                }
                this.inited = true;
                System.out.println("初始化完成");
            } catch (UnknownHostException e) {
                LOGGER.error(String.format("init search client err:=>msg:[%s]", e.getMessage()), e);
                if (transportClient != null) {
                    this.transportClient.close();
                }
            }
        }
        return transportClient;
    }

    public synchronized void close(){
        if (this.transportClient!=null)
            this.transportClient.close();
    }

    public TransportClient getTransportClient() {
        return transportClient;
    }

    public void setTransportClient(TransportClient transportClient) {
        this.transportClient = transportClient;
    }

    public boolean isClient_transport_sniff() {
        return client_transport_sniff;
    }

    public void setClient_transport_sniff(boolean client_transport_sniff) {
        this.client_transport_sniff = client_transport_sniff;
    }

    public String getTransport_addresses() {
        return transport_addresses;
    }

    public void setTransport_addresses(String transport_addresses) {
        this.transport_addresses = transport_addresses;
    }

    public String getCluster_name() {
        return cluster_name;
    }

    public void setCluster_name(String cluster_name) {
        this.cluster_name = cluster_name;
    }
}
